// pages/category/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftList: [],
    rightList: [],
    activeIndex: 0, //左右切换下标
    scrollYs: 0 //回到顶部
  },
  shopList: [],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getleftshop()
    //存储，缓存
    let shopList = wx.getStorageSync('shopList');
    console.log(shopList);
    //如果是空，就进行数据请求
    if (!shopList) {
      this.getleftshop()
    } else {
      //如果存在数据的话需要判断下是否过期
      if ((+new Date() - shopList.time) > 100000) {
        this.getleftshop()
      } else {
        this.shopList = shopList.data
        //左侧数据
        let leftList = this.shopList.map(item => item.cat_name);
        //右侧数据
        let rightList = this.shopList[0].children;

        this.setData({
          leftList,
          rightList

        })
      }

    }
  },

  //商品数据
  getleftshop: async function () {
    let {
      data
    } = await wx.categoryApi.categorydata()
    // console.log(data);
    this.shopList = data.message;
    //将数据存到本地存储
    wx.setStorageSync('shopList', {
      time: +new Date(),
      data: this.shopList
    })
    //左侧数据
    let leftList = this.shopList.map(item => item.cat_name);
    //右侧数据
    let rightList = this.shopList[0].children;

    this.setData({
      leftList,
      rightList

    })
    console.log(rightList);
  },

  //左侧导航样式切换
  clickbt(e) {
    console.log(e);
    let inde = e.currentTarget.dataset.index
    //右侧数据
    let rightList = this.shopList[inde].children;
    this.setData({
      rightList,
      activeIndex: inde,
      scrollYs: 0

    })
  }

})